/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package co.umb.MAS.Models.DAOs;

import co.umb.MAS.Models.ConnectionDB;
import co.umb.MAS.Models.PersonaModel;
import co.umb.MAS.Models.ValorRespuestaModel;
import co.umb.MAS.Models.conexionDbPool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alexander
 */
public class PostgresHistoriaClinicaDao implements AccionesDao {

    private Connection cnn = null;

    @Override
    public ArrayList consultarTodos() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Object conultar(String criterio) {
        ArrayList<ValorRespuestaModel> valRes = new ArrayList<>();
        try {
            cnn = conexionDbPool.obtenerConexion();
            String query = "select * from t_valor_respuesta where id_persona="
                    + Integer.parseInt(criterio);
            PreparedStatement ps = cnn.prepareStatement(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                ValorRespuestaModel vr = new ValorRespuestaModel();
                vr.setValor_respuesta(rs.getInt(1));
                if (rs.getString(2) != null && rs.getString(2) != "") {
                    vr.setId_pregunta(rs.getInt(2));
                }
                vr.setId_persona(rs.getInt(3));
                if (rs.getString(4) != null && rs.getString(4) != "") {
                    vr.setValor(rs.getString(4));
                }
                if (rs.getString(5) != null && rs.getString(5) != "") {
                    vr.setId_opcion_rta(rs.getInt(5));
                }
                valRes.add(vr);
            }
            cnn.close();
        } catch (SQLException ex) {
            Logger.getLogger(PostgresPersonaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return valRes;
    }

    public String consultaIDHistoriaClinica(String criterio) {
        String hc = "";
        try {
            cnn = conexionDbPool.obtenerConexion();
            String query = "select (valor_rta) from t_valor_respuesta where"
                    + " (id_pregunta=18) and (id_persona=" + Integer.parseInt(criterio) + ")";
            PreparedStatement ps = cnn.prepareStatement(query);
            ResultSet rs = ps.executeQuery();
            if (rs != null) {
                while (rs.next()) {
                    hc = rs.getInt(1) + "";
                }
            }
            cnn.close();
        } catch (SQLException ex) {
            Logger.getLogger(PostgresPersonaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return hc;
    }

    @Override
    public boolean crear(Object item) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public boolean registrarValorRespuesta(int idPregunta, int idPersona, String valorRTA) {
        boolean successful = false;
        try {
            PreparedStatement prt = null;
            cnn = conexionDbPool.obtenerConexion();
            String query = "insert into t_valor_respuesta(id_valor_rta,id_pregunta,id_persona,valor_rta) values("
                    + "nextval('t_valor_respuesta_id_valor_rta_seq')"
                    + "," + idPregunta + "," + idPersona + ",'" + valorRTA + "')";

            prt = cnn.prepareStatement(query);
            successful = prt.executeUpdate() == 1 ? true : false;

        } catch (SQLException ex) {
            Logger.getLogger(PostgresHistoriaClinicaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return successful;
    }

    public boolean registrarOpcionRespuesta(int idPersona, int idOpcionRTA) {
        boolean successful = false;
        try {
            PreparedStatement prt = null;
            cnn = conexionDbPool.obtenerConexion();
            String query = "insert into t_valor_respuesta(id_valor_rta,id_persona,id_opcion_rta) values("
                    + "nextval('t_valor_respuesta_id_valor_rta_seq')"
                    + "," + idPersona + "," + idOpcionRTA + ")";

            prt = cnn.prepareStatement(query);
            successful = prt.executeUpdate() == 1 ? true : false;

        } catch (SQLException ex) {
            Logger.getLogger(PostgresHistoriaClinicaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return successful;
    }

    @Override
    public void eliminar() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void editar() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
